import numpy as np
from sklearn import preprocessing
from sklearn.ensemble import ExtraTreesClassifier
from sklearn import metrics
from sklearn.linear_model import LogisticRegression
from sklearn.model_selection import GridSearchCV
import seaborn as sns
import matplotlib.pyplot as plt

print('1. 正在加载数据集...')
dataset = np.loadtxt('pima-indians-diabetes.csv', delimiter=",")

print('2. 正在对数据集预处理...')
X = dataset[:, 0:7]
y = dataset[:, 8]
scaled_X = preprocessing.scale(X)  # 幅度缩放
normalized_X = preprocessing.normalize(X)  # 归一化
standardized_X = preprocessing.scale(X)  # 标准化

model = ExtraTreesClassifier()  # xtraTreesClassifier是一种基于集成学习的分类器，它使用决策树作为基本估计器，并利用随机子集特征进行训练
model.fit(X, y)
print('4. 集成学习模型学习完毕...')

# 特征重要度
print('=============特征重要度输出=================')
feature_importances = model.feature_importances_
feature_importances_str = ' '.join(map(str, feature_importances.tolist()))
print(feature_importances_str)

# 超参数调优
param_grid = {'penalty': ['l1', 'l2', 'elasticnet'],
              'C': [0.1, 1, 10]}
grid_search = GridSearchCV(LogisticRegression(), param_grid, cv=5)
grid_search.fit(X, y)
best_model = grid_search.best_estimator_
print('5. 超参数调优完成，已获得最佳逻辑斯特回归模型...')

print('6. 模型正在学习...')
best_model.fit(X, y)

expected = y
predicted = model.predict(X)
print('7. 模型推理完毕...')

print('8. 模型评估完毕...')
print(metrics.classification_report(expected, predicted))

print('=========================================')
print('CONFUSION MATRIX')
confusion_matrix = metrics.confusion_matrix(expected, predicted)
print(confusion_matrix)

# # 绘制热力图
# sns.heatmap(confusion_matrix, annot=True, cmap="YlGnBu",fmt='d',annot_kws={'size': 20})
# plt.xlabel('Predicted labels')
# plt.ylabel('True labels')
# plt.title('Confusion Matrix')
# plt.show()

'''
结果总结
1. 决策树集成学习分类器全部分类正确
2. 逻辑斯特回归模型的结果正确率为70%左右
'''
